const models = require("../db/mysqldb/index");
const { Op } = require("sequelize");

class DataSystemDictTypeModel {
  /**
   * @description 通过 dict_name 查找数据，不包含删除数据
   * @param {string} dict_name
   * @returns
   */
  static async findBySystemDictTypeName({ dict_name }) {
    return await models.sys_dict_type.findOne({
      where: {
        dict_name,
        del_flag: "0",
      },
    });
  }
  /**
   * @description 通过 dict_type 查找数据，不包含删除数据
   * @param {string} dict_type
   * @param {string} dict_id
   * @returns
   */
  static async findBySystemDictTypeCode({ dict_type }) {
    return await models.sys_dict_type.findOne({
      where: {
        dict_type,
        del_flag: "0",
      },
    });
  }
  /**
   * @description 通过 dict_name id 查找数据，不包含删除数据
   * @param {string} dict_name
   * @param {string} dict_id
   * @returns
   */
  static async findBySystemDictTypeNameNeId({ dict_name, dict_id }) {
    return await models.sys_dict_type.findOne({
      where: {
        dict_name,
        del_flag: "0",
        dict_id: {
          [Op.ne]: dict_id,
        },
      },
    });
  }
  /**
   * @description 通过 dict_type id 查找数据，不包含删除数据
   * @param {string} dict_type
   * @param {string} dict_id
   * @returns
   */
  static async findBySystemDictTypeCodeId({ dict_type, dict_id }) {
    return await models.sys_dict_type.findOne({
      where: {
        dict_type,
        del_flag: "0",
        dict_id: {
          [Op.ne]: dict_id,
        },
      },
    });
  }
  /**
   * @description 通过 dict_id 查找数据，不包含删除数据
   * @param {string} dict_id
   * @returns
   */
  static async findBySystemdDictTypeId({ dict_id }) {
    return await models.sys_dict_type.findOne({
      where: {
        dict_id,
        del_flag: "0",
      },
    });
  }
  /**
   * @description create
   * @param {String} dict_type
   * @param {String} dict_name
   * @param {Number} status
   * @param {String} remark
   * @param {String} op_user_id
   * @returns
   */
  static async create({ dict_name, dict_type, status, remark, op_user_id }) {
    return await models.sys_dict_type.create({
      dict_type,
      dict_name,
      status,
      remark,
      create_by: op_user_id,
      create_time: Date.now(),
    });
  }
  /**
   * @description 更新
   * @param {Number} dict_id
   * @param {String} dict_type
   * @param {String} dict_name
   * @param {Number} status
   * @param {String} remark
   * @param {String} op_user_id
   * @returns
   */
  static async update({
    dict_id,
    dict_type,
    dict_name,
    status,
    remark,
    op_user_id,
  }) {
    return await models.sys_dict_type.update(
      {
        dict_id,
        dict_type,
        dict_name,
        status,
        remark,
        update_by: op_user_id,
        update_time: Date.now(),
      },
      {
        where: { dict_id },
      }
    );
  }
  /**
   * @description 删除
   * @param {*} op_user_id
   * @param {*} dict_id
   * @returns
   */
  static async delete({ op_user_id, dict_id }) {
    return await models.sys_dict_type.update(
      {
        del_flag: "2",
        update_by: op_user_id,
      },
      {
        where: { dict_id },
      }
    );
  }

  /**
   * @description 批量删除
   * @param {Number} op_user_id
   * @param {Array} ids
   * @returns
   */
  static async deleteByIds({ op_user_id, ids }) {
    return await models.sys_dict_type.update(
      {
        del_flag: "2",
        update_by: op_user_id,
      },
      {
        where: {
          dict_id: {
            [Op.in]: ids,
          },
        },
      }
    );
  }
  /**
   * @description 获取字典信息
   * @param {String} dict_name
   * @param {String} dict_type
   * @param {String} status
   * @param {String} limit
   * @param {String} size
   * @returns
   */
  static async list({ limit, size, dict_name, dict_type, status }) {
    let querySql = "";
    if (dict_name) {
      querySql += ` and dict_name like '%${dict_name}%'`;
    }
    if (dict_type) {
      querySql += ` and dict_type like '%${dict_type}%'`;
    }
    if (status) {
      querySql += ` and status = ${status}`;
    }
    let sql = `SELECT
        dict_id,
        dict_name,
        dict_type,
        status,
        remark,
        create_time
      FROM
        sys_dict_type WHERE del_flag = '0' ${querySql}
      ORDER BY
      create_time desc,update_time DESC limit ${(limit - 1) * size}, ${size};`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    const count = await models.sequelize.query(
      `SELECT count(1) as count
    FROM
      sys_dict_type WHERE del_flag = '0' ${querySql}`,
      { replacements: ["active"], type: models.sequelize.QueryTypes.SELECT }
    );
    return {
      result,
      count,
    };
  }
  /**
   * @description 获取所有字典信息
   * @returns
   */
  static async all() {
    let sql = `SELECT
        dict_id,
        dict_name,
        dict_type,
        status,
        remark,
        create_time
      FROM
        sys_dict_type WHERE del_flag = '0'
      ORDER BY
        create_time DESC,
        update_time DESC`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }
  /**
   * @description 获取所有字典信息
   * @returns
   */
  static async findList({ dict_name, dict_type, status }) {
    let querySql = "";
    if (dict_name) {
      querySql += ` and dict_name like '%${dict_name}%'`;
    }
    if (dict_type) {
      querySql += ` and dict_type like '%${dict_type}%'`;
    }
    if (status) {
      querySql += ` and status = ${status}`;
    }
    let sql = `SELECT
        dict_id,
        dict_name,
        dict_type,
        status,
        remark,
        create_time
      FROM
        sys_dict_type WHERE del_flag = '0' ${querySql}
      ORDER BY
        create_time DESC,
        update_time DESC`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }
}
module.exports = {
  DataSystemDictTypeModel,
};
